﻿using System;
using System.Linq;
using System.Windows.Forms;
using NanoERP.Infrastructure;
using NanoERP.Business.BaseInfo;
using NanoERP.Model;
using System.Collections.Generic;
using System.Drawing;
using System.Text.RegularExpressions;

namespace NanoERP.BaseInfo
{
    [Module("FrmFlowInfo")]
    public partial class FrmFlowInfo : ModuleForm
    {
        readonly FlowinfoBLL _flowinfoBLL;
        List<Flowphase> _flowphaseList;
        Flowinfo _flowinfo;
        public FrmFlowInfo()
        {
            InitializeComponent();

            _flowinfoBLL = new FlowinfoBLL();
            _flowphaseList = new List<Flowphase>();

            cboPhasename.SelectedIndex = 0;
            QueryInfo();
        }

        /// <summary>
        /// 
        /// </summary>
        void QueryInfo()
        {
            dgvList.AutoGenerateColumns = false;
            dgvList.DataSource = _flowinfoBLL.Query();

            dgvList_CellClick(dgvList, new DataGridViewCellEventArgs(1, 0));
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void dgvList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1) return;

            txtPhaseuser1.Text = string.Empty;
            txtPhaseuser2.Text = string.Empty;
            txtPhaseuser3.Text = string.Empty;
            txtPhaseuser4.Text = string.Empty;
            txtPhaseuser5.Text = string.Empty;
            _flowinfo = dgvList.Rows[e.RowIndex].DataBoundItem as Flowinfo;

            lbFlowname.Text = _flowinfo.Flowname;
            cboPhasename.SelectedIndex = _flowinfo.Phasecount.Value - 1;

            _flowphaseList = _flowinfoBLL.QueryFlowphase(_flowinfo.Id);
            _flowphaseList.ForEach(r =>
            {
                switch (r.Phasename)
                {
                    case "一级审核": txtPhaseuser1.Text = r.Phaseuser; break;
                    case "二级审核": txtPhaseuser2.Text = r.Phaseuser; break;
                    case "三级审核": txtPhaseuser3.Text = r.Phaseuser; break;
                    case "四级审核": txtPhaseuser4.Text = r.Phaseuser; break;
                    case "五级审核": txtPhaseuser5.Text = r.Phaseuser; break;
                }
            });

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            int index = cboPhasename.SelectedIndex;
            _flowinfo.Phasecount = index + 1;
            _flowphaseList.ForEach(r => r.Isfinish = r.Phasenum.Equals(index + 1) ? 1 : 0);
            _flowinfoBLL.Edit(_flowinfo, _flowphaseList);

            MessageBox.Show("单据审核环节设置保存成功!", "提示消息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

        private void btnAllBills_Click(object sender, EventArgs e)
        {
            int index = cboPhasename.SelectedIndex;

            this.Invoke(new MethodInvoker(() =>
            {
                foreach (DataGridViewRow d in dgvList.Rows)
                {
                    var o = d.DataBoundItem as Flowinfo;
                    o.Phasecount = index + 1;

                    _flowphaseList.ForEach(r => { r.Flowid = o.Id; r.Isfinish = r.Phasenum.Equals(index + 1) ? 1 : 0; });

                    _flowinfoBLL.Edit(o, _flowphaseList);
                }
            }));

            MessageBox.Show("当前单据规则已成功应用到所有业务单据.", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void cboPhasename_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = cboPhasename.SelectedIndex;
            panel1.Enabled = index >= 0;
            panel2.Enabled = index > 0;
            panel3.Enabled = index > 1;
            panel4.Enabled = index > 2;
            panel5.Enabled = index > 3;
        }

        void panel_EnabledChanged(object sender, EventArgs e)
        {
            Panel panel = sender as Panel;
            if (panel.Enabled)
            {
                foreach (var txtBox in panel.Controls)
                {
                    if (Regex.IsMatch(txtBox.GetType().Name, @"(?i)Label|Button")) continue;
                    ((TextBox)txtBox).BackColor = SystemColors.Window;
                }
            }
        }

        void btnSelect1_Click(object sender, EventArgs e)
        {
            CreateFormView.BindStaffForm(txtPhaseuser1, true);

            DoFlowphase("一级审核", txtPhaseuser1.Text, 1);
        }

        void btnSelect2_Click(object sender, EventArgs e)
        {
            CreateFormView.BindStaffForm(txtPhaseuser2, true);

            DoFlowphase("二级审核", txtPhaseuser2.Text, 2);
        }

        void btnSelect3_Click(object sender, EventArgs e)
        {
            CreateFormView.BindStaffForm(txtPhaseuser3, true);

            DoFlowphase("三级审核", txtPhaseuser3.Text, 3);
        }

        void btnSelect4_Click(object sender, EventArgs e)
        {
            CreateFormView.BindStaffForm(txtPhaseuser4, true);

            DoFlowphase("四级审核", txtPhaseuser4.Text, 4);
        }

        void btnSelect5_Click(object sender, EventArgs e)
        {
            CreateFormView.BindStaffForm(txtPhaseuser5, true);

            DoFlowphase("五级审核", txtPhaseuser5.Text, 5);
        }

        /// <summary>
        /// 单据审核流程
        /// </summary>
        /// <param name="phaseName">阶段名称</param>
        /// <param name="phaseUser">阶段操作用户</param>
        /// <param name="phasenum">阶段操作编号</param>
        void DoFlowphase(string phaseName, string phaseUser, int? phasenum)
        {
            if (phaseUser.Length == 0)
                return;

            var phase = _flowphaseList.FirstOrDefault(r => r.Phasename.Equals(phaseName));
            if (phase == null)
                _flowphaseList.Add(new Flowphase { Flowid = _flowinfo.Id, Phasenum = phasenum, Phasename = phaseName, Phaseuser = phaseUser });
            else
            {
                phase.Phaseuser = phaseUser;
                phase.Phasenum = phasenum;
            }
        }
    }
}
